﻿var UsernameModel = function () {
    var self = this;
    self.display = ko.observable("bdimperio");
    self.url = ko.computed(function () {
        return "http://ws-" + self.display() + "7/";
    });
    self.filesystem = ko.computed(function () {
        return "file://ws-" + self.display() + "7/c$/";
    });
    self.netshare = ko.computed(function () {
        return "\\\\ws-" + self.display() + "7\\c$\\";
    });
    self.users = ko.observableArray([]);
    self.saveuser = function () {
        if (!ko.utils.arrayFirst(self.users(), function (item) {
            return self.display().toLowerCase() === item.toLowerCase();
        })) {
            self.users.push(self.display());
        }

    };
    self.getCurrentIfSaved = ko.computed({
        read: function () {
            if (ko.utils.arrayFirst(self.users(), function (item) {
                return item.toLowerCase() === self.display().toLowerCase();
            })) {
                return self.display();
            }
            return undefined;
        }, write: function (value) {
            if (value !== undefined) {
                self.display(value);
            }
            
        }

    });
    var storage = new Usernamestorage();
    if (storage) {
        self.users(storage.get());
        self.users.subscribe(function (array) {
            console.log('users changed');
            storage.set(array);
        });
    }
};